package com.heer.gymnasium.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.R;
import com.heer.gymnasium.adapter.SiteDetailsBookedAdapter;
import com.heer.gymnasium.adapter.SiteDetailsType1Adapter;
import com.heer.gymnasium.adapter.SiteDetailsTypeAdapter;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.util.ScreenUtils;
import com.heer.gymnasium.util.ViewUtils;
import com.heer.model.SitePlaceBookedBean;
import com.heer.model.SitePlaceCateBean;
import com.heer.model.SitePlaceInfoBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by admin on 2017/7/11.
 */

public class PlaceCateListActivity extends BaseActivity {

    @BindView(R.id.lv_placecatelist)
    ListView list;

    SitePlaceCateBean sitepcBean;
    private SVProgressHUD svProgressHUD;

    private SiteDetailsTypeAdapter mAdapter;
    private SiteDetailsType1Adapter mAdapter1;


    public static void startPlaceList(Context context, SitePlaceCateBean bean)
    {
        Intent intent=new Intent(context,PlaceCateListActivity.class);
        Bundle bundle=new Bundle();
        bundle.putSerializable("sitepc",bean);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }


    @Override
    protected void init() {
        super.init();
        setContentView(R.layout.activity_palcecate_list);
    }

    @Override
    protected void initView() {
        super.initView();
        svProgressHUD = new SVProgressHUD(mContext);
        sitepcBean= (SitePlaceCateBean) getIntent().getSerializableExtra("sitepc");
        titleBuilder.setTitleText(sitepcBean.getCate_name());
        if (sitepcBean.getIsding().equals("1")) {

            list.setAdapter(mAdapter = new SiteDetailsTypeAdapter(mContext));
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long ids) {
                    SitePlaceInfoBean.Ticket item = mAdapter.getItem(position);
                    SiteReservedInfoActivity.toReservedInfo(mContext, item.getId(), item.getPrice());

                }
            });
        } else  {
            list.setAdapter(mAdapter1 = new SiteDetailsType1Adapter(mContext));
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long ids) {
                    SiteReservedActivity.siteReserved(mContext, sitepcBean.getId(), mAdapter1.getItem(position), mAdapter1.getItemList());
                    //                        SiteReservedActivity.siteReserved(mContext, mAdapter.getItem(position).getId());
                }
            });
        }

    }

    @Override
    protected void loadData(boolean loadmore) {
        svProgressHUD.showWithStatus("加载中", SVProgressHUD.SVProgressHUDMaskType.BlackCancel);
        if (sitepcBean.getIsding().equals("1")) {
            mAppHelper.apiService().getSitePlaceInfoList(sitepcBean.getId()).enqueue(new BaseCallback<ApiResponse<List<SitePlaceInfoBean>>>() {
                @Override
                public void onResponse(Call<ApiResponse<List<SitePlaceInfoBean>>> call, Response<ApiResponse<List<SitePlaceInfoBean>>> response) {
                    super.onResponse(call, response);
                    svProgressHUD.dismiss();
                    if (response.isSuccessful() && response.body().isSccessful()) {
                        List<SitePlaceInfoBean> placeList = response.body().getData();
                        if (null != placeList && placeList.size() > 0) {
                            List<SitePlaceInfoBean.Ticket> tickets = new ArrayList<>();
                            for (SitePlaceInfoBean item : placeList) {
                                for (SitePlaceInfoBean.Ticket ticket : item.getTicket()) {
                                    tickets.add(ticket);
                                }
                            }
                            mAdapter.setItems(tickets);
                            mAdapter.setCount(tickets.size());



                        }
                    }
                    if (response.body().isTokenExpire()) {
                        toLogin();
                    }

                }

                @Override
                public void onFailure(Call<ApiResponse<List<SitePlaceInfoBean>>> call, Throwable t) {
                    super.onFailure(call, t);
                    svProgressHUD.dismiss();
                }
            });
        } else {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = sDateFormat.format(new Date());
            mAppHelper.apiService().getSitePlaceBookedList(sitepcBean.getId(), date, 30).enqueue(new BaseCallback<ApiResponse<List<SitePlaceBookedBean>>>() {
                @Override
                public void onResponse(Call<ApiResponse<List<SitePlaceBookedBean>>> call, Response<ApiResponse<List<SitePlaceBookedBean>>> response) {
                    super.onResponse(call, response);
                    svProgressHUD.dismiss();
                    if (response.isSuccessful() && response.body().isSccessful()) {
                        List<SitePlaceBookedBean> placeList = response.body().getData();
                        if (null != placeList && placeList.size() > 0) {
                            mAdapter1.setItems(placeList);
                            mAdapter1.setCount(placeList.size());
                        }
                    }
                    if (response.body().isTokenExpire()) {
                        toLogin();
                    }
                }

                @Override
                public void onFailure(Call<ApiResponse<List<SitePlaceBookedBean>>> call, Throwable t) {
                    super.onFailure(call, t);
                    svProgressHUD.dismiss();
                }
            });
        }

    }

}
